package com.sky.db.app.controller;


import com.sky.db.app.controller.entity.DbRequest;
import com.sky.db.meta.DbHelper;
import com.sky.db.meta.entity.ColumnInfo;
import com.sky.db.meta.entity.TableInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String Hello() {
        return "Hello SpringBoot";
    }

    /**
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取所有用户数据库", notes = "不用传参的")
    @RequestMapping(value = "/getUserDbList", method = RequestMethod.GET)
    public List<String> getUserDbList() throws Exception {
        List<String> dbList = new DbHelper().getUserDbList();
        return dbList;
    }

    @ApiOperation(value = "获取指定库下的所有表", notes = "传库名")
    @RequestMapping(value = "/getTableListByDbName", method = RequestMethod.POST)
    public List<TableInfo> getTableListByDbName(DbRequest dbRequest) {
        List<TableInfo> tableList = new DbHelper().getTableListByDbName(dbRequest.getDbName());
        return tableList;
    }

    @ApiOperation(value = "指定表基本信息", notes = "入参库名,表名")
    @RequestMapping(value = "/getTableInfoSimple", method = RequestMethod.POST)
    public TableInfo getTableInfoByDbNameAndTbName(DbRequest dbRequest) {
        TableInfo tableInfo = new DbHelper().getTableInfoByDbNameAndTbName(dbRequest.getDbName(), dbRequest.getTableName());
        return tableInfo;
    }

    @ApiOperation(value = "指定表所有字段列表", notes = "入库名,表名")
    @RequestMapping(value = "/getColumnListByTableName", method = RequestMethod.POST)
    public List<ColumnInfo> getColumnListByTableName(DbRequest dbRequest) {
        List<ColumnInfo> columnInfoList = new DbHelper().getColumnListByTableName(dbRequest.getDbName(), dbRequest.getTableName());
        return columnInfoList;
    }

    @ApiOperation(value = "指定表详细信息,含字段列表", notes = "入参库名,表名")
    @RequestMapping(value = "/getTableInfo", method = RequestMethod.POST)
    public TableInfo getTableInfo(DbRequest dbRequest) {

        TableInfo tableInfo = new DbHelper().getTableInfoByDbNameAndTbName(dbRequest.getDbName(), dbRequest.getTableName());

        List<ColumnInfo> columnInfoList = new DbHelper().getColumnListByTableName(dbRequest.getDbName(), dbRequest.getTableName());

        tableInfo.setColumnInfoList(columnInfoList);

        return tableInfo;
    }


}
